package cn.edu.nju.gitlab.presenter;

import android.graphics.Color;

import java.util.ArrayList;
import java.util.List;

import cn.edu.nju.gitlab.activity.HomeworkActivity;
import cn.edu.nju.gitlab.data.TeacherData;
import cn.edu.nju.gitlab.fragment.ScoreFragment;
import cn.edu.nju.gitlab.model.QuestionScore;
import cn.edu.nju.gitlab.model.Score;
import cn.edu.nju.gitlab.model.Student;
import rx.Subscription;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;


/**
 * Created by lpt on 2017/6/14.
 */

public class ScorePresenter {
    private TeacherData repository;
    private HomeworkActivity view;
    private Subscription subscription;
    private int[] scoreRange = {0, 60, 70, 80, 90, 100};
    private int[] colors = {Color.RED, Color.BLUE, Color.CYAN, Color.GREEN, Color.GRAY, Color.YELLOW};

    public ScorePresenter(TeacherData repository, HomeworkActivity view) {
        this.repository = repository;
        this.view = view;
    }

    public void getLineChartData(String username, String password, int assignmentId) {
        subscription = repository.getScore(username, password, assignmentId)
                .subscribeOn(Schedulers.io())
                .onErrorReturn(new Func1<Throwable, Score>() {
                    @Override
                    public Score call(Throwable throwable) {
                        throwable.printStackTrace();
                        view.showError();
                        return null;
                    }
                })
                .subscribe(new Action1<Score>() {
                    @Override
                    public void call(Score score) {

                        List<QuestionScore> questions = score.getQuestions();

                        double sum=0;
                        int count=0;
                        double[] data = new double[6];
                        for (QuestionScore questionScore : questions) {

                            data[0] = data[1] = data[2] = data[3] = data[4] =data[5]= 0;
                            for (Student student : questionScore.getStudents()) {
                                double value = student.getScore();
                                for (int i = 0; i < scoreRange.length - 1; i++) {

                                    count++;
                                    sum+=value;
                                    if (value > scoreRange[i] && value <= scoreRange[i + 1]) {
                                        data[i]++;
                                    }
                                }
                            }

                        }

                        data[5]=Double.valueOf(sum)/Double.valueOf(count);

                        System.out.println(data[0]);
                        view.showLineChart(data);
                    }

                });
    }

    public void onStop() {
        if (subscription != null && !subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
    }



}
